
var isFirst = true;

$(document).ready(function(e) {	
	initialSystemData();	
	setInterval ("initialSystemData()", 600000);//10分钟刷新一次系统常用数据
	localStorage.setItem('trcolor','bgcolor="#EEEEEE"');
});


function initialSystemData(){
	if(isFirst == true){
		localStorage.removeItem('clientClassification');
		localStorage.removeItem('sourceOfChannel');
		localStorage.removeItem('productType');
		localStorage.removeItem('spaceStatus');
		localStorage.removeItem('space');
		localStorage.removeItem('contracttemplate');
		isFirst = false;
	}
	initialProvinceAnCity();
	initialSpaceStatus();
	initialSpace();

}


function initialSpace(){
	var spaces = {};
	$.ajax({
        url: '/euc/system/space',
        data:'',
        beforeSend: function(request) {
            request.setRequestHeader("token",localStorage.getItem('memberToken'));
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	try{
	        	$.each(result,function(index,data){	
	        		spaces[data.id] = data;  
	   			});
	        	var spaceJson = JSON.stringify(spaces);
	        	localStorage.setItem('space',spaceJson);
	        	console.log(spaceJson);
        	}catch(e){
        		console.log('error 1');
        	}
        },
        error: function () {
        	console.log('error 1');
        }  
    });
}


function initialSpaceStatus(){
	var spaceStatuses = {};
	$.ajax({
        url: '/euc/system/spacestatus',
        data:'',
        beforeSend: function(request) {
        	request.setRequestHeader("token",localStorage.getItem('memberToken'));
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	try{
	        	$.each(result,function(index,data){	
	        		spaceStatuses[data.statusId] = data;  
	   			});
	        	var spaceStatusJson = JSON.stringify(spaceStatuses);
	        	localStorage.setItem('spaceStatus',spaceStatusJson);
        	}catch(e){
        		/*alert(e.message);*/
        	}
        },
        error: function () {
        	/*alert("error");*/
        }  
    });
}


function initialProvinceAnCity(){
	var provinces = {};
	var cities = {};
	$.ajax({
        url: '/euc/system/province',
        data:'',
        beforeSend: function(request) {
        	request.setRequestHeader("token",localStorage.getItem('memberToken'));
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	try{
	        	$.each(result,function(index,data){	
	        		provinces[data.provinceId] = data;           		
	           		$.each(data.cities,function(index,city){
	           			cities[city.cityId] = city;	
	    			});
	   			});
	        	var provinceJson = JSON.stringify(provinces);
	        	var cityJson = JSON.stringify(cities);        	
	        	localStorage.setItem('province',provinceJson);
	        	localStorage.setItem('city',cityJson);
        	}catch(e){
        		/*alert(e.message);*/
        	}
        },
        error: function () {
        	/*alert("error");*/
        }  
    });
}
